package com.ubtechinc.cruzr.launcher2.model;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.callback.AppListener;
import com.ubtechinc.cruzr.launcher2.callback.SettingListener;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.model.helper.LoadHelper;
import com.ubtechinc.cruzr.launcher2.presenter.AppPresenter;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;

public class AppManger {
    private AppListener mAppListener;
    private BatteryManager mBatteryManager;
    private StatusManager mStatusManager;
    private boolean isAppDeleteState;
    private SettingListener mSttingsListener;
    private AppPresenter mPresenter;
    private long mLastClickTime;

    private static class ModelHolder {
        static AppManger APP = new AppManger();
    }

    public static AppManger get() {
        return ModelHolder.APP;
    }

    public void init() {
        mBatteryManager = new BatteryManager();
        mStatusManager = new StatusManager(LauncherApplication.getContext());
    }

    public void attachListener(AppPresenter presenter, AppListener listener) {
        mPresenter = presenter;
        mAppListener = listener;
        mBatteryManager.attachListener(listener);
    }

    public void updateBattery() {
        mBatteryManager.updateBattery();
    }

    public BatteryManager getBatteryManager() {
        return mBatteryManager;
    }

    public void loadApps(boolean isinit, boolean isadmini) {
        PackageManager pm = LauncherApplication.getContext().getPackageManager();
        LoadHelper.load(LauncherApplication.getContext(), pm, mAppListener, isinit, isadmini);
    }

    public void enableDeleteModel(boolean isdelete) {
        if (isAppDeleteState != isdelete) {
            isAppDeleteState = isdelete;
        }
    }

    public boolean isAppDeleteState() {
        return isAppDeleteState;
    }

    public boolean isThidStatus() {
        return mStatusManager.isContainsThirdAppStatus();
    }

    public void removeThirdAppStatus() {
        mStatusManager.removeThirdAppStatus();
    }

    public SettingListener getSttingsListener() {
        return mSttingsListener;
    }

    public void setSttingsListener(SettingListener mSttingsListener) {
        this.mSttingsListener = mSttingsListener;
    }

    public void openActivity(String pkg, String activityname, boolean isthird) {
        if (Math.abs(System.currentTimeMillis() - mLastClickTime) < 500) {
            return;
        }
        if (AppManger.get().isAppDeleteState()) {
            return;
        }
        if (TextUtils.isEmpty(pkg) || TextUtils.isEmpty(activityname)) {
            return;
        }
        Log.e("launcher", "rqh do action position = " + activityname);
        try {
            ComponentName componentName = new ComponentName(pkg, activityname);
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setComponent(componentName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            LauncherApplication.getContext().startActivity(intent);
            mLastClickTime = System.currentTimeMillis();
            if (isthird) {
                //  boolean isrecord = ThirdStateHelper.getUsesPermission(LauncherApplication.getContext(), pkg);
                //  LogUtils.e("rqh getUsesPermission  isrecord = " + isrecord);
                // if (isrecord) {
                mStatusManager.setThirdAppStatus(); // 所有应用都需要加状态
                mAppListener.onThirdAppOpen();
                // }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void syncTtsCommand(String pkg) {
        if (LoadHelper.isLoading) {
            TtsManager.get().speech(LauncherApplication.getContext().getString(R.string.loading_app));
        } else {
            AppInfo info = LoadHelper.getInfoByPkg(pkg);
            if (info != null) {
                if (info.isAdminiApp) {
                    boolean admini = mPresenter != null && mPresenter.isAdmini();
                    if (admini) {
                        openActivity(info.packagename, info.activityname, !info.isSystemApp());
                    } else {
                        TtsManager.get().speech(LauncherApplication.getContext().getString(R.string.sign_in_remind));
                    }
                } else {
                    openActivity(info.packagename, info.activityname, !info.isSystemApp());
                }
            } else {
                LogUtils.e("app not found");
                TtsManager.get().speech(LauncherApplication.getContext().getString(R.string.app_not_found));
            }
        }
    }

    public void startDeleteAinimation() {
        mStatusManager.startDeleteAinimation();
    }

    public void stopDeleteAinimation() {
        mStatusManager.stopDeleteAinimation();
    }

    public float getCurrentFloat() {
        return mStatusManager.getCurrentFloat();
    }
}
